package com.wocai.jrt.orgadmin.device.service.impl;

import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.vteba.common.exception.ServiceException;
import com.vteba.service.generic.impl.MyBatisServiceImpl;
import com.vteba.tx.jdbc.mybatis.spi.BaseDao;
import com.wocai.jrt.orgadmin.device.bean.PosPadReqBean;
import com.wocai.jrt.orgadmin.device.bean.PosPadResBean;
import com.wocai.jrt.orgadmin.device.dao.PosPadDao;
import com.wocai.jrt.orgadmin.device.model.PosPad;
import com.wocai.jrt.orgadmin.device.model.PosPadBean;
import com.wocai.jrt.orgadmin.device.service.spi.PosPadService;
import com.wocai.jrt.orgadmin.employee.dao.EmployeeDao;
import com.wocai.jrt.orgadmin.employee.model.Employee;
import com.wocai.jrt.orgadmin.investor.bean.EmpBreifBean;
import com.wocai.jrt.orgadmin.investor.bean.MatchReqBean;

/**
 * 设备相关的service业务实现。
 * 
 * @author yinlei
 * @date 2015-8-17 17:03:44
 */
@Named
public class PosPadServiceImpl extends MyBatisServiceImpl<PosPad, PosPadBean, String> implements
		PosPadService {
	private static final Logger LOGGER = LoggerFactory.getLogger(PosPadServiceImpl.class);
	// 添加了方法后，记得改为private
	private PosPadDao posPadDao;
	@Inject
	private EmployeeDao employeeDao;//尹雷，该service和EmployeeService循环引用

	@Override
	@Inject
	public void setBaseDao(BaseDao<PosPad, PosPadBean, String> posPadDao) {
		this.baseDao = posPadDao;
		this.posPadDao = (PosPadDao) posPadDao;
	}

	@Override
	public List<PosPadResBean> getPosPadList(PosPadReqBean ppReqBean) {
		List<PosPadResBean> list = posPadDao.getPosPadList(ppReqBean);
		return list;
	}
	
	/**
	 * 获取机构下所有理财师
	 * 
	 * @param String
	 *            id
	 * @return List<GroupBean>
	 */
	@Override
	public List<EmpBreifBean> getOrgEmployeeList(String tenantIdentifier, String employeeKey) {
		MatchReqBean mBean = new MatchReqBean();
		mBean.setEmployeeKey(employeeKey);
		mBean.setOrgId(tenantIdentifier);
		List<EmpBreifBean> list = posPadDao.getOrgEmployeeList(mBean);
		return list;
	}
	
	@Override
	public long getPosPadCount(PosPadReqBean ppReqBean) {
		long size = posPadDao.getPosPadCount(ppReqBean);
		return size;
	}

	@Override
	public int pullbackDevice(String id) {
		try {
			Employee emp = new Employee();
			emp.setPosPadId(id);
			emp = employeeDao.unique(emp);
			if (null == emp) {
				return 0;
			}
			emp.setPosPadId("");
			int r = employeeDao.updateById(emp);
			if (0 >= r) {
				LOGGER.error("update employee error");
				throw new ServiceException("update employee error");
			}
			int l = posPadDao.pullbackDevice(id);
			if (0 >= l) {
				LOGGER.error("update device error");
				throw new ServiceException("update device error");
			}
		} catch (Exception e) {
			LOGGER.error("update device error, msg=[{}].", e.getMessage());
			throw new ServiceException("update device error", e);
		}
		return 1;
	}

	@Override
	public int distributeDevice(PosPadReqBean model) {
		try {
			PosPad posPad = new PosPad();
			posPad.setEmployeeId(model.getEmployeeId());
			posPad = posPadDao.unique(posPad);
			if (null != posPad) {
				return 2;
			}
			Employee emp = new Employee();
			emp.setEmployeeId(model.getEmployeeId());
			emp.setPosPadId(model.getId());
			int r = employeeDao.updateById(emp);
			if (0 >= r) {
				LOGGER.error("update employee error");
				throw new ServiceException("update employee error");
			}
			int l = posPadDao.distributeDevice(model);
			if (0 >= l) {
				LOGGER.error("update device error");
				throw new ServiceException("update device error");
			}
		} catch (Exception e) {
			LOGGER.error("update device error, msg=[{}].", e.getMessage());
			throw new ServiceException("update device error", e);
		}
		return 1;
	}

	@Override
	public PosPad queryPosPadInfo(PosPad params) {
		return posPadDao.queryPosPadInfo(params);
	}

	@Override
	public List<PosPadResBean> queryPosPadAuto(PosPadReqBean reqBean) {
		return posPadDao.queryPosPadAuto(reqBean);
	}
}
